package com.cgy.utils.elasticsearch.single;

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.support.replication.ReplicationResponse;
import org.elasticsearch.action.update.UpdateResponse;

import java.util.Objects;

/**
 * @author CH
 */
@Slf4j
public class ElasticSearchDelete {
    /**
     *
     * @param response
     */
    public static boolean response(DeleteResponse response) {
        if (Objects.nonNull(response)) {
            if (response.getResult() == DocWriteResponse.Result.NOT_FOUND) {
                log.error("不存在该文档，请检查参数！");
                return true;
            }
            log.info("文档已删除！");
            ReplicationResponse.ShardInfo shardInfo = response.getShardInfo();
            if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
                log.error("部分分片副本未处理");
                return false;
            }
            if (shardInfo.getFailed() > 0) {
                for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {
                    String reason = failure.reason();
                    log.error("失败原因：{}", reason);
                }
                return false;
            }
        }
        return true;
    }
}
